package servlet;

import entity.Admin;
import service.AdminService;
import service.impl.AdminServiceImpl;
import util.Pager;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.crypto.Data;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class AdminServlet extends HttpServlet {
    private AdminService service =new AdminServiceImpl();
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String type = request.getParameter("type");
        try {
            //通过反射获取方法
            Method method = this.getClass().getDeclaredMethod(type, HttpServletRequest.class, HttpServletResponse.class);
            method.setAccessible(true);
            try {
                //调用这个方法
                method.invoke(this, request, response);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
    }
    public void findAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String currPageNo = request.getParameter("currPageNo");
        int pageNo = 1;
        if (currPageNo !=null){
            pageNo = Integer.parseInt(currPageNo);
        }
        List<Admin> list = service.findAllAdmin();
        Pager<Admin> pager = new Pager<>();
        pager.setList(list);
        pager.setCurrPageNo(pageNo);
        List<Admin> subList=pager.getList();
        request.setAttribute("list",subList);
        request.setAttribute("pager",pager);
        request.getRequestDispatcher("adminList.jsp").forward(request,response);
    }
    //添加管理员
    public void addAdmin(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取参数
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String name = request.getParameter("name");
        String address = request.getParameter("address");
        String birthday = request.getParameter("birthday");
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        Date date=null;
        try{
            date = sdf.parse(birthday);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        //调用后台方法
        Admin admin = new Admin(1,username,password,name,address,date);
        service.addAdmin(admin);
        //页面跳转
        response.sendRedirect("/adminServlet?type=findAll");
    }
    //删除管理员
    protected void delAdmin(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取参数
        String id = request.getParameter("id");
        //调用后台方法
        service.delAdmin(Integer.parseInt(id));
        //页面跳转
        response.sendRedirect("/adminServlet?type=findAll");
    }
    //查看管理员
    protected void findOne(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id = request.getParameter("id");
        service.delAdmin(Integer.parseInt(id));
        request.getRequestDispatcher("admin/show.jsp").forward(request,response);
    }
    protected void findById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.获取参数
        String id = request.getParameter("id");
        //2.调用后台方法
        Admin admin = service.findAdminById(Integer.parseInt(id));
        //3.页面跳转
        request.setAttribute("admin",admin);
        request.getRequestDispatcher("admin/upd.jsp").forward(request,response);
    }
    //修改管理员
    public void updAdmin(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取参数
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String name = request.getParameter("name");
        String address = request.getParameter("address");
        String birthday = request.getParameter("birthday");
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        Date date=null;
        try{
            date = sdf.parse(birthday);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        //调用后台方法
        Admin admin = new Admin(1,username,password,name,address,date);
        service.updAdmin(admin);
        //页面跳转
        response.sendRedirect("/adminServlet?type=findAll");
    }
}
